<?php
	defined('_JEXEC') or die('Restricted access');
	require_once(CUTEENGINE_BASE_ACTION_PATH."base_action_class.php");
    require_once(CUTEENGINE_BASE_STEP_PATH."base_step_class.php");
	//
    // t_action_send_mail
    //
	class t_action_send_mail extends t_base_action
	{
        // property declaration
		// protected     
		
		// property declaration
		// public
		// L
		const language_position_per_sent = 1;
		const language_position_per_not_sent = 2;
		const language_position_per_empty_to_field = 3;
		const language_position_per_empty_from_field = 4;
		const language_position_per_empty_subject_field = 5;
		const language_position_per_empty_message_field	= 6;	
		const language_position_last = 7;		
		// P
		const proper_properties_position_per_to_name_part = 0;
		const proper_properties_position_per_to_domain_part = 1;
		const proper_properties_position_per_from_name_part = 2;
		const proper_properties_position_per_from_domain_part = 3;
		const proper_properties_position_per_subject = 4;
		const proper_properties_position_per_message = 5;
        const proper_properties_position_last = 6;
        // R
        const required_fields_count = 3;
      
        // method declaration
    	// protected
        // G
		static function get_help_proper_property($a_index)
		{
			$local_return = 'NO HELP AVAILABLE';
			/*switch ($a_index) 
            {
                case self::proper_properties_position_:
					$local_return = '';
				break;                
			}*/
			return $local_return;
		}
        static function get_default_proper_property($a_index)
		{
			$local_return = '';
			//echo "a_index: $a_index<br>";
			/*switch ($a_index) 
            {
				case self::proper_properties_position_per_:
					$local_return = '';
				break;				
			}*/
			return $local_return;
		}		
        protected function get_proper_properties_position_last()
        {
            return self::proper_properties_position_last;
        }
		protected function get_required_fields_count()
        {
            return self::required_fields_count;
        }
        

		// I
        protected function initialize_action()
        {		

			if (! $this->initialized)
			{
                parent::initialize_action();
				
				
                //$this->initialized = true;

			}
			return $this->initialized;
		}		
        protected function initialize_step_execution()
        {		

			$local_step = $this->steps_list[$this->current_step_index];
			$local_index = $this->current_step_index;
			$local_step->initialize();
			$local_class = get_class($local_step);
			
			if (($local_index == t_base_action::step_criteria) || ($local_index == t_base_action::step_result))
			{
				$this->custom_execute_step($local_step);                        
				
				
			}    
			else
			{
				$this->execute_step($local_step);                                            
			}
		}	
        protected function custom_execute_step($a_step)        
        {
            //echo "this->current_step_index: $this->current_step_index<br>";
            $local_next_step = $this->steps_list[$this->current_step_index + 1];
			$local_field = $a_step->field;
			//echo "local_field->name: ".$local_field->name."<br>"; 
			if ($local_field)
			{
				$local_start_tag = $local_field->get_start_tag();
				$local_field_class = get_class($local_field);    
			}			
		    $local_class = get_class($a_step);
           
            
            switch ($local_class) 
            {
				/*********** t_step_ajax_get ***********/
				case t_step_ajax_get:
					$a_step->code = "
	var local_parent = ".t_cuteengine::javascript_instance_name."['".$local_start_tag->name."'];
    var local_argoument = local_parent.prepare_childs_values_to_send('".$local_start_tag->name."');
    if (!local_argoument)
        return false;
    ". $this->get_content_loader_code($local_next_step->code,true,true);
					
                break;                

					
                /*********** t_step_ajax_put ***********/
                case t_step_ajax_put:

                break;                
            }
        }
        // method declaration
    	// public
        public function __construct($a_parent_component,$a_parent_part,$a_common_properties_str, $a_proper_properties_str) 
		{
            parent::__construct($a_parent_component,$a_parent_part,$a_common_properties_str, $a_proper_properties_str);
            //echo "count:".count($this->fields_list);
			// steps
            $this->steps_list[t_base_action::step_start] = t_base_step::create_step_from_string('|t_step_ajax_start|'.$this->get_action_name().'_start|'.
              $this->fields_list[t_base_action::step_start].'|'); 
            $this->steps_list[t_base_action::step_criteria] = t_base_step::create_step_from_string('|t_step_ajax_get|'.$this->get_action_name().'_criteria|'.
              $this->fields_list[t_base_action::step_criteria].'|');   
			$this->steps_list[t_base_action::step_result] = t_base_step::create_step_from_string('|t_step_ajax_put|'.$this->get_action_name().'_res|'.
              $this->fields_list[t_base_action::step_result].'|');   
			
		}
        // D
		public function do_action_from_ajax()
        {
			$local_parameter = parent::do_action_from_ajax();
            $this->send_mail($local_parameter);
            
        }
		// I
		public function is_executable()
		{
			$local_to = $this->proper_properties_array[self::proper_properties_position_per_to_name_part]."@".
				$this->proper_properties_array[self::proper_properties_position_per_to_domain_part];	
			return ($local_to != '');
		}
		//S
		public function send_mail($a_parameter)
        {	
            global $g_ambient_namespace;
			$local_result = new t_content_loader_result;
            $local_parameters_array = $g_ambient_namespace->remove_hide_separator(explode(t_url_parameter::separator_level_1,$a_parameter->get_argument_value(3)));
         //   print_r($local_parameters_array);
			$local_to = $this->proper_properties_array[self::proper_properties_position_per_to_name_part]."@".
				$this->proper_properties_array[self::proper_properties_position_per_to_domain_part];
			$local_from = $this->proper_properties_array[self::proper_properties_position_per_from_name_part]."@".
				$this->proper_properties_array[self::proper_properties_position_per_from_domain_part];
			$local_subject = $this->proper_properties_array[self::proper_properties_position_per_subject];
			$local_message = $this->proper_properties_array[self::proper_properties_position_per_message];
			//echo "local_to: $local_to - local_subject: $local_subject - local_message: $local_message - local_from: $local_from<br>";
			if ($local_to == '')
			{
				$local_to = $local_parameters_array[0];
				$local_start_index = 1;
			}	
			else
			{
				// TO AVOID TO SPECIFY DESTINATION IN URL PARAMETERS, IT CONSIDERS FIST URL PARAMETER AS SUBJECT MAIL
				$local_start_index = 0;
			}
			if ($local_from == '@')
			{
				//echo "local_parameters_array[local_start_index]: ".$local_parameters_array[$local_start_index] ; 
				$local_from = $local_parameters_array[$local_start_index]; 	
			}	
			if ($local_subject == '')
			{
				$local_subject = $local_parameters_array[($local_start_index+1)]; 
			}	
			if ($local_message == "")
				$local_message = $local_parameters_array[($local_start_index+2)]; 
			//echo "local_to: $local_to - local_subject: $local_subject - local_message: $local_message - local_from: $local_from<br>";
			// CHEK MAIL VALUES
			$local_is_to_send = false;
			if ($local_to == '')
			{
				$local_error_msg = $this->lang[self::language_position_per_empty_to_field];
			} 
			else if ($local_from == '')
			{
				$local_error_msg = $this->lang[self::language_position_per_empty_from_field];
			} 
			else if ($local_subject == '')
			{
				$local_error_msg = $this->lang[self::language_position_per_empty_subject_field];
			} 
			else if ($local_message == "")
			{
				$local_error_msg = $this->lang[self::language_position_per_empty_message_field];
			} 
			else 
				$local_is_to_send = true;	
			// SEND MAIL
			if ($local_is_to_send)
			{
				$headers  = 'MIME-Version: 1.0' . "\r\n";
                $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
				if (mail($local_to, $local_subject, $local_message,$headers ."From: ". $local_from))
				{
					$local_result->succeeded = true;
					$local_error_msg = $this->lang[self::language_position_per_sent];
				}	
				else
				{
					$local_result->succeeded = false;
					$local_error_msg = $this->lang[self::language_position_per_not_sent];
				}
			}	
			else
			{
				$local_result->succeeded = false;
				
			}	
			if ($this->enable_language)
				$local_result->error_msg = $local_error_msg;
			echo $local_result->get_json_string();	

		}
		public function get_language_position_last()
        {
            return self::language_position_last;
        }

    }
?>    